package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author linexsong
 */
@Service
@Slf4j
public class DishServicesImpl implements DishService {
    private DishMapper dishMapper;
    private DishFlavorMapper dishFlavorMapper;

    private SetmealDishMapper setmealDishMapper;

    @Autowired
    public void setSetmealDishMapper(SetmealDishMapper setmealDishMapper) {
        this.setmealDishMapper = setmealDishMapper;
    }

    @Autowired
    public void setDishMapper(DishMapper dishMapper) {
        this.dishMapper = dishMapper;
    }

    @Autowired
    public void setDishFlavorMapper(DishFlavorMapper dishFlavorMapper) {
        this.dishFlavorMapper = dishFlavorMapper;
    }

    /**
     * 新增菜品和对应的口味
     * 开启事务
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithFlavor(DishDTO dto) {
        // 菜品表插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.insert(dish);
        // 获取 insert 语句生成的主键值
        Long id = dish.getId();

        // 口味表插入多条数据
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors == null || flavors.isEmpty()) {
            return;
        }

        flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
        dishFlavorMapper.insertBatch(flavors);


    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = dishFlavorMapper.pageQuery(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Long> ids) {
        // 判断菜品是否能删除 -- 起售中?
        ids.forEach(id -> {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                // 起售中
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        // -- 被套餐关联?
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && !setmealIds.isEmpty()) {
            // 被关联
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 删除菜品
        //for (Long id : ids) {
        //    dishMapper.deleteById(id);
        //    // 删除菜品关联的口味
        //    dishFlavorMapper.deleteByDishId(id);
        //}
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        // 查询菜品
        Dish dish = dishMapper.getById(id);
        // 查询口味
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        // 封装到 vo
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dto) {
        // 修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.update(dish);

        // 删除原有口味
        dishFlavorMapper.deleteByDishId(dish.getId());
        // 重新插入口味
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors == null || flavors.isEmpty()) {
            return;
        }
        flavors.forEach(flavor -> {
            flavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertBatch(flavors);
    }
}
